package com.wing.collection;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * @author heweiye
 * @date 2021/5/10
 * @description: TODO
 */
public class MyList implements List {

    private Object[] DEFAULT_ELEMENT_DATA = new Object[10];

    private Object[] elementData;

    private int size = 0;

    private int modCount = 0;

    private int maxSize = Integer.MAX_VALUE - 8;

    public MyList() {
        this.elementData = DEFAULT_ELEMENT_DATA;
    }

    public MyList(int capacity) {
        this.elementData = new Object[capacity];
    }


    @Override
    public int size() {
        return elementData.length;
    }

    @Override
    public boolean isEmpty() {
        return elementData.length > 0;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }

    @Override
    public Iterator iterator() {
        return null;
    }

    @Override
    public Object[] toArray() {
        return new Object[0];
    }

    @Override
    public boolean add(Object o) {
        //判断是否需要扩容
        ensureCapacity(size + 1);
        elementData[size++] = o;
        modCount++;
        return true;
    }

    private void ensureCapacity(int minCapacity) {
        int capacity = calculateCapacity(minCapacity);
        if (minCapacity - elementData.length > 0) {
            //进行扩容
            grow(capacity);
        }
    }

    private void grow(int minCapacity) {
        int oldCapacity = elementData.length;
        //扩容0.5倍
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        //扩容的0.5倍小于最小需要扩容数量
        if (newCapacity - minCapacity < 0) {
            newCapacity = minCapacity;
        }
        if (newCapacity > maxSize) {
            //处理扩容最大边界问题
            newCapacity = hugeCapacity(minCapacity);
        }

        elementData = Arrays.copyOf(elementData, newCapacity);
    }

    private int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) {
            //最大值+1就是负值
            throw new OutOfMemoryError();
        } else {
            return minCapacity > maxSize ? Integer.MAX_VALUE : maxSize;
        }
    }

    private int calculateCapacity(int minCapacity) {
        if (elementData == DEFAULT_ELEMENT_DATA) {
            return Math.max(minCapacity, 10);
        }
        return minCapacity;
    }

    @Override
    public boolean remove(Object o) {


        modCount++;
        return true;
    }

    @Override
    public boolean addAll(Collection c) {
        return false;
    }

    @Override
    public boolean addAll(int index, Collection c) {
        return false;
    }

    @Override
    public void clear() {

    }

    @Override
    public Object get(int index) {
        rangeCheck(index);
        return elementData[index];
    }

    private void rangeCheck(int index) {
        if (index < 0 || index > elementData.length - 1) {
            throw new IllegalArgumentException();
        }
    }

    @Override
    public Object set(int index, Object element) {
        return null;
    }

    @Override
    public void add(int index, Object element) {
        rangeCheck(index);
        ensureCapacity(size + 1);
        //移动数据
        System.arraycopy(elementData, index, elementData, index + 1, size - index);

        elementData[index] = element;

        size++;

        modCount++;
    }

    @Override
    public Object remove(int index) {
        rangeCheck(index);

        Object oldElement = elementData[index];
        //计算要移动的数据的下标
        int numMove = size - index - 1;

        if (numMove > 0) {
            //在数组往前移动对应的数据
            System.arraycopy(elementData, index + 1, elementData, index, numMove);
        }
        elementData[--size] = null;
        modCount++;
        return oldElement;
    }

    @Override
    public int indexOf(Object o) {
        return 0;
    }

    @Override
    public int lastIndexOf(Object o) {
        return 0;
    }

    @Override
    public ListIterator listIterator() {
        return null;
    }

    @Override
    public ListIterator listIterator(int index) {
        return null;
    }

    @Override
    public List subList(int fromIndex, int toIndex) {
        return null;
    }

    @Override
    public boolean retainAll(Collection c) {
        return false;
    }

    @Override
    public boolean removeAll(Collection c) {
        return false;
    }

    @Override
    public boolean containsAll(Collection c) {
        return false;
    }

    @Override
    public Object[] toArray(Object[] a) {
        return new Object[0];
    }
}
